﻿using System.Text.RegularExpressions;

// ReSharper disable CheckNamespace
namespace System.Text
// ReSharper restore CheckNamespace
{
    public static class StringExtensions
    {
        private static string WildcardToRegex(string pattern)
        {
            return "^" + Regex.Escape(pattern).
                               Replace(@"\*", ".*").
                               Replace(@"\?", ".") + "$";
        }

        public static bool MatchesWildcard(this string s, string wildcard)
        {
            if (s == "*") return true;
            var regex = WildcardToRegex(wildcard);
            return Regex.IsMatch(s, regex);
        }

        public static string ReplaceAt(this string s, char replaceTo, int at)
        {
            char[] chars = s.ToCharArray();
            chars[at] = replaceTo;
            return new string(chars);
        }
    }
}